package com.zerdoor.controller;

import com.zerdoor.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoji on 2015/1/27.
 * 平台配置管理
 */
@Controller
@RequestMapping("setting")
public class SettingController {
    @Autowired
    private SettingService settingService;

    /**
     * 支付宝设置页面（编号为101001;101002,101003）
     * @param model
     * @return
     */
    @RequestMapping(value = "alipaySetting", method = RequestMethod.GET)
    public String alipaySetting(Model model){
        //支付宝账号101001
        Map<String,Object> map1 = settingService.getConfigById(101001);
        if(map1 != null){
            model.addAttribute("alipayAccount",map1.get("value_cn"));
        }
        //合作者身份id 101002
        Map<String,Object> map2 = settingService.getConfigById(101002);
        if(map2 != null){
            model.addAttribute("alipayCardId",map2.get("value_cn"));
        }
        //安全效验码key 101003
        Map<String,Object> map3 = settingService.getConfigById(101003);
        if(map3 != null){
            model.addAttribute("alipayValidKey",map3.get("value_cn"));
        }
        return "/setting/alipaySetting";
    }

    /**
     * 保存支付宝
     * @param alipayAccount 支付宝账号
     * @param alipayCardId 合作者身份id
     * @param alipayValidKey 安全效验码key
     * @return
     */
    @RequestMapping(value = "saveAlipay", method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> saveAlipay(String alipayAccount,String alipayCardId,String alipayValidKey){
        Map<String, Object> map1 = getConfigMap(101001,"支付宝账号","支付宝账号",alipayAccount,alipayAccount,"支付宝账号");
        Map<String, Object> map2 = getConfigMap(101002,"合作者身份id","合作者身份id",alipayCardId,alipayCardId,"合作者身份id");
        Map<String, Object> map3 = getConfigMap(101003,"安全效验码key","安全效验码key",alipayValidKey,alipayValidKey,"安全效验码key");
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map1.put("","");
            settingService.updateConfig(map1);
        } catch (Exception e) {
            map.put("errormsg","保存支付宝账号失败！");
        }
        try {
            settingService.updateConfig(map2);
        } catch (Exception e) {
            map.put("errormsg","保存安全效验码key失败！");
        }
        try {
            settingService.updateConfig(map3);
        } catch (Exception e) {
            map.put("errormsg","保存合作者身份id失败！");
        }
        return map;
    }

    /**
     * 作品手续费设置页面
     * @param model
     * @return
     */
    @RequestMapping(value = "workFeeSetting", method = RequestMethod.GET)
    public String  workFeeSetting(Model model){
        //作品手续费
        Map<String,Object> map1 = settingService.getConfigById(1);
        if(map1 != null){
            model.addAttribute("feeCn",map1.get("value_cn"));
            model.addAttribute("feeEn",map1.get("value_en"));
        }
        return "/setting/workFeeSetting";
    }

    /**
     * 保存手续费设置
     * @param feeCn 中文手续费
     * @param feeEn 英文手续费
     * @return
     */
    @RequestMapping(value = "saveWorkFee", method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> saveWorkFee(String feeCn,String feeEn){
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = getConfigMap(1,"作品手续费","Work fee",feeCn,feeEn,"作品手续费");
        try {
            settingService.updateConfig(map1);
        } catch (Exception e) {
            map.put("errormsg","保存作品手续费失败！");
        }
        return map;
    }

    /**
     * 竞赛手续费设置页面
     * @param model
     * @return
     */
    @RequestMapping(value = "raceSetting", method = RequestMethod.GET)
    public String  raceSetting(Model model){
        //竞赛手续费
        Map<String,Object> map1 = settingService.getConfigById(4);
        if(map1 != null){
            model.addAttribute("feeCn",map1.get("value_cn"));
            model.addAttribute("feeEn",map1.get("value_en"));
        }
        //竞赛推广费
        Map<String,Object> map2 = settingService.getConfigById(5);
        if(map2 != null){
            model.addAttribute("puparCn",map2.get("value_cn"));
            model.addAttribute("puparEn",map2.get("value_en"));
        }
        return "/setting/raceSetting";
    }

    /**
     * 保存手续费设置
     * @param feeCn 中文手续费
     * @param feeEn 英文手续费
     * @return
     */
    @RequestMapping(value = "saveRaceSet", method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> saveRaceSet(String feeCn,String feeEn,String puparCn,String puparEn){
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = getConfigMap(4,"竞赛手续费","race fee",feeCn,feeEn,"竞赛手续费");
        Map<String, Object> map2 = getConfigMap(5,"竞赛推广费"," pupar",puparCn,puparEn,"竞赛推广费");
        try {
            settingService.updateConfig(map1);
        } catch (Exception e) {
            map.put("errormsg","保存竞赛手续费失败！");
        }

        try {
            settingService.updateConfig(map2);
        } catch (Exception e) {
            map.put("errormsg","保存竞赛推广费失败！");
        }
        return map;
    }


    /**
     * 项目手续费设置页面
     * @param model
     * @return
     */
    @RequestMapping(value = "itemSetting", method = RequestMethod.GET)
    public String  itemSetting(Model model){
        //项目手续费
        Map<String,Object> map1 = settingService.getConfigById(6);
        if(map1 != null){
            model.addAttribute("feeCn",map1.get("value_cn"));
            model.addAttribute("feeEn",map1.get("value_en"));
        }
        //项目推广费
        Map<String,Object> map2 = settingService.getConfigById(7);
        if(map2 != null){
            model.addAttribute("puparCn",map2.get("value_cn"));
            model.addAttribute("puparEn",map2.get("value_en"));
        }
        return "/setting/itemSetting";
    }

    /**
     * 保存项目手续费设置
     * @param feeCn 中文手续费
     * @param feeEn 英文手续费
     * @param puparCn 中文推广费
     * @param puparEn 英文推广费
     * @return
     */
    @RequestMapping(value = "saveItemSet", method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> saveItemSet(String feeCn,String feeEn,String puparCn,String puparEn){
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = getConfigMap(6,"项目手续费","Item fee",feeCn,feeEn,"项目手续费");
        Map<String, Object> map2 = getConfigMap(7,"项目推广费","Item Promotion expenses",puparCn,puparEn,"项目推广费");
        try {
            settingService.updateConfig(map1);
        } catch (Exception e) {
            map.put("errormsg","保存竞赛手续费失败！");
        }

        try {
            settingService.updateConfig(map2);
        } catch (Exception e) {
            map.put("errormsg","保存竞赛推广费失败！");
        }
        return map;
    }

    /**
     * 托管定金比列设置页面
     * @param model
     * @return
     */
    @RequestMapping(value = "depositSetting", method = RequestMethod.GET)
    public String  depositSetting(Model model){
        //作品手续费
        Map<String,Object> map1 = settingService.getConfigById(2);
        if(map1 != null){
            model.addAttribute("depositCn",map1.get("value_cn"));
            model.addAttribute("depositEn",map1.get("value_en"));
        }
        return "/setting/depositSetting";
    }

    /**
     * 保存托管定金比例
     * @param depositCn 中文手续费
     * @param depositEn 英文手续费
     * @return
     */
    @RequestMapping(value = "saveDepositProportion", method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> saveDepositProportion(String depositCn,String depositEn){
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = getConfigMap(2,"托管定金","Work fee",depositCn,depositEn,"托管定金比例");
        try {
            settingService.updateConfig(map1);
        } catch (Exception e) {
            map.put("errormsg","保存托管定金比例失败！");
        }
        return map;
    }




    /**
     * 合同条例设置页面
     * @param model
     * @return
     */
    @RequestMapping(value = "contractSetting", method = RequestMethod.GET)
    public String  contractSetting(Model model){
        //作品手续费
        Map<String,Object> map1 = settingService.getConfigById(3);
        if(map1 != null){
            model.addAttribute("contractCn",map1.get("value_cn"));
            model.addAttribute("contractEn",map1.get("value_en"));
        }
        return "/setting/contractSetting";
    }

    /**
     * 保存合同条例
     * @param contractCn 中文合同条例
     * @param contractEn 英文合同条例
     * @return
     */
    @RequestMapping(value = "saveContractSet", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveContractSet(String contractCn,String contractEn){
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map1 = getConfigMap(3,"ACG合同条列","ACG terms  contract",contractCn,contractEn,"ACG合同条列");
        try {
            settingService.updateConfig(map1);
        } catch (Exception e) {
            map.put("errormsg","保存合同条例失败！");
        }
        return map;
    }

    /**
     * 构造config对象
     * @param configId 配置编号
     * @param configCn 配置中文名称
     * @param configEn 配置英文名称
     * @param valueCn 值
     * @param valueEn 值
     * @param description 描述
     * @return
     */
    private Map<String, Object> getConfigMap(int configId,String configCn,String configEn,
                                             String valueCn,String valueEn,String description){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("configId",configId);
        map.put("configCn",configCn);
        map.put("configEn",configEn);
        map.put("valueCn",valueCn);
        map.put("valueEn",valueEn);
        map.put("description",description);
        return map;
    }


}
